import './Scss/pay.scss';
var msg = location.href;
var statusInfo = '正在支付 ';
var statusAdd = ['·', '··', '···'];
var statusIndex = 0;
var statusObj = document.getElementById('paystatus');
var blackbuy = document.getElementById('blackbuy');
var statusTimer = setInterval(function() {
    statusObj.innerHTML = statusInfo + statusAdd[statusIndex];
    statusIndex++;
    if (statusIndex == statusAdd.length)
        statusIndex = 0;
    }
, 400);
setTimeout(function() {
    blackbuy.style.display = 'initial';
}, 6000);

if (msg.indexOf('?') > -1 && msg.indexOf('&') > -1) {
    msg = msg.split('?')[1].split('&');
    var config = {};
    for (var i = 0; i < msg.length; i++) {
        let name = msg[i].split('=')[0];
        let value = msg[i].split('=')[1];
        if (name == '')
            continue;
        if (name == 'package')
            value = value + '=' + msg[i].split('=')[2];
        if (name == 'class' || name == 'school' || name == 'name')
            value = decodeURIComponent(value.replace(/\+/g, ''));
        if (name == 'fee')
            value = '￥' + value / 100 + '元';
        if (name == 'order_id')
            value = value.substr(0, 10) + '...';

        if (name == 'urlto') {
            blackbuy.onclick = function(e) {
                location.href = value;
                e.preventDefault();
                e.stopPropagation();
            }
        } else {
            config[name] = value;
        }
    }
    console.log(config);
    function onBridgeReady() {
        WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId": config.appId,
            "timeStamp": config.timeStamp,
            "nonceStr": config.nonceStr,
            "package": config.package,
            "signType": config.signType,
            "paySign": config.paySign
        }, function(res) {
            var paystatus = document.getElementById('paystatus');
            document.getElementById('blackbuy').style.display = 'initial';
            document.getElementById('paybox').style.marginTop = '10%';
            clearInterval(statusTimer);
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                paystatus.innerHTML = '支付成功';
                document.getElementById('paytip').style.display = 'initial';
                var payinfo = document.getElementById('payinfo');
                payinfo.style.display = 'initial';
                payinfo = payinfo.children[1];
                payinfo.children[0].children[1].innerHTML = config.order_id;
                payinfo.children[1].children[1].innerHTML = config.school;
                payinfo.children[2].children[1].innerHTML = config.class;
                payinfo.children[3].children[1].innerHTML = config.name;
                payinfo.children[4].children[1].innerHTML = config.fee;
            } else {
                paystatus.innerHTML = '支付失败';
                document.getElementById('payreload').style.display = 'initial';
            }
        });
    }
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady();
    }
}
